// pages/bsbx/bsbx.js
const httpRequest = require('../../utils/http_utils.js')
const dateutil = require('../../utils/date_util.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    datalist: [], //数据集合
    DisplayName: '', //用户姓名
    RoleName: '', //用户职业
    imgs: '', //图片
    info: '', //提示信息
    uploading: false, //加载提示
    title: '报修',
    showmore: false,
    showzanwu: false,
    menulist: [],
    customHeight:0, //自定义高度
  },

  click: function(e) {
    wx.navigateTo({
      url: '../details/details?RowGuid=' + e.currentTarget.dataset.rowguid,
    })
  },

  initdata: function() {
    var that = this;

    var pageSize = 10; //每页显示条数
    var counter = 1; //计数器
    var pageStart = 0; //开始数据条数
    var CoditionArray = new Array();
    var parameter = {
      rolename: '*',
      pagesize: pageSize,
      currentpage: counter,
      where: JSON.stringify(CoditionArray),
      orderby: "order by WorkTime desc",
      type: 2
    }
    httpRequest.GetValidateRequest(httpRequest.apiInfo.GetWorkOrderSearch2Api, parameter, function(data) {
        console.log(typeof(data));
        console.log(data.flag == '暂无数据！');
        if (data.totalNum == 0 || data.flag == '暂无数据！') {
          that.setData({
            showmore: false,
            showzanwu: true
          })
        } else {
          for (var i = 0; i < JSON.parse(data.pageData).length; i++) {
            var WorkOrderStateText = 'datalist[' + i + '].WorkOrderStateText';
            var Description = 'datalist[' + i + '].Description';
            var WorkTime = 'datalist[' + i + '].WorkTime';
            var RowGuid = 'datalist[' + i + '].RowGuid';
            that.setData({
              [WorkOrderStateText]: JSON.parse(data.pageData)[i].WorkOrderStateText,
              [Description]: JSON.parse(data.pageData)[i].Description,
              [WorkTime]: dateutil.formatTime(JSON.parse(data.pageData)[i].WorkTime),
              [RowGuid]: JSON.parse(data.pageData)[i].RowGuid,
            })
          }
          console.log(JSON.stringify(that.data.datalist))

          that.setData({
            uploading: false,
            showmore: true,
            showzanwu: false
          })
        }

      },
      function(error) {

      })
  },

  tiaozhuan(e) {
    var rowguid = e.currentTarget.dataset.rowguid; //获取菜单rowguid
    var url = e.currentTarget.dataset.url; //获取按钮跳转地址
    console.log(url + '?ParentGuid=' + rowguid)
    if (url.indexOf('repair/repair') != -1) {
      if (wx.getStorageSync('UserInfo').AppLogin.RoleName == '客服人员') {
        wx.navigateTo({
          url: url
        })
      } else {
        this.setData({
          info: '报修目前只对客服人员开放'
        })
      }
    } else {
      wx.navigateTo({
        url: url
      })
    }
  },

  mytask: function() {
    wx.navigateTo({
      url: '/pages/workorderlist/workorderlist',
    })
  },

  scrollToUpper: function() { //下拉刷新
    this.setData({
      uploading: true
    })
    this.initdata();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    this.setData({
      customHeight: (105 + 87 + 40 + app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2
    })
    console.log('---------导航栏高度--------' + this.data.customHeight)

    if (options.ParentGuid != undefined && options.ParentGuid != '') {
      var menu_list = this.data.menulist;
      var storage_list = wx.getStorageSync('UserInfo').AppLogin.AppModuleDTO.filter(function(item) {
        return item.ParentGuid === options.ParentGuid;
      });

      for (var i = 0; i < storage_list.length; i++) {
        var param = {
          RowGuid: storage_list[i].RowGuid,
          Name: storage_list[i].ModuleName,
          Icon: storage_list[i].BigIconAddress,
          ModuleUrl: storage_list[i].ModuleUrl
        }
        menu_list.push(param)
      }
      //根据RowGuid去重
      const RowGuid = 'RowGuid';
      menu_list = menu_list.reduce((all, next) => all.some((atom) => atom[RowGuid] == next[RowGuid]) ? all : [...all, next], []);
      this.setData({
        menulist: menu_list
      })
      console.log('menulist---', this.data.menulist)
    }
    this.setData({
      DisplayName: wx.getStorageSync("UserInfo").AppLogin.DisplayName,
      RoleName: wx.getStorageSync("UserInfo").AppLogin.RoleName
    })

    this.refreshView = this.selectComponent("#refreshView")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.initdata();
    if (wx.getStorageSync("UserInfo").AppLogin.Sex == 1) { //根据用户性别动态显示图片
      this.setData({
        imgs: "../images/nan.png"
      })
    } else {
      this.setData({
        imgs: "../images/nv.png"
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  //触摸开始
  handletouchstart: function(event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function(event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function(event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function(event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function(event) {
    this.refreshView.onPageScroll(event)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    setTimeout(() => {
      this.initdata();
      this.refreshView.stopPullRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})